package com.zy.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zy.reggie.common.R;
import com.zy.reggie.entity.Category;
import com.zy.reggie.entity.Setmeal;
import com.zy.reggie.service.CategoryService;
import com.zy.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;



/**
 * 分类管理
 */
@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private SetmealService setmealService;

    @PostMapping
    public R<String> save(@RequestBody Category category) {
        log.info("category:{}",category);
        categoryService.save(category);
        return R.success("分类添加成功");
    }

    /**
     * 分页查询
     * @param page 当前页数
     * @param pageSize 当前页数的记录条数
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize) {
        //创建分页构造器
        Page<Category> pageInfo = new Page<>(page,pageSize);
        //创建条件构造器
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件
        lambdaQueryWrapper.orderByAsc(Category::getSort);
        //分页查询
        categoryService.page(pageInfo,lambdaQueryWrapper);
        return R.success(pageInfo);
    }


    @DeleteMapping
    public R<String> delete(Long id) {
        log.info("删除分类id：{}",id);
        //categoryService.removeById(id);
        categoryService.remove(id);
        return R.success("删除分类成功");

    }
    @PutMapping
    public R<String> update(@RequestBody Category category) {
        log.info("修改的分类信息{}",category);
        categoryService.updateById(category);
        return R.success("修改分类成功");
    }

    /**
     * 根据条件查询分类
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R<List> list(Category category) {
        //构造条件构造器
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //添加条件
        lambdaQueryWrapper.eq(category.getType() != null,Category::getType,category.getType());
        //进行排序
        lambdaQueryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        //进行查询
        List<Category> list = categoryService.list(lambdaQueryWrapper);
        return R.success(list);

    }

}
